<template>
  <view class="container">
    
    <!-- 表单组件 -->
    <view class="form-wrapper">
      <u-form :model="form" ref="uForm" label-width="140rpx">
        <u-form-item label="等级名称" prop="name">
          <u-input v-model="form.name" placeholder="请输入等级名称" />
        </u-form-item>
      </u-form>
      <u-form :model="form" ref="uForm" label-width="140rpx">
        <u-form-item label="等级权重" prop="weight">
          <u-input type="number" v-model="form.weight" placeholder="请输入等级权重" />
        </u-form-item>
      </u-form>
      <u-form :model="form" ref="uForm" label-width="140rpx">
        <u-form-item label="升级条件" prop="expend_money">
          <u-input type="number" v-model="form.upgrade.expend_money" placeholder="请输入升级所需消费金额" />
        </u-form-item>
      </u-form>
    </view>
    <!-- 操作按钮 -->
    <view class="footer">
      <view class="btn-wrapper">
        <view class="btn-item btn-item-main" :class="{ disabled }" @click="handleSubmit()">保存</view>
      </view>
    </view>
  </view>
</template>

<script>
  import Verify from '@/utils-shop/verify'
  import * as GradeApi from '@/apishop/grade'

  // 表单字段元素
  const form = {
    equity: {discount:"0"},
    name: '',
	sort: 100,
	status: 1,
	upgrade: {expend_money: 10000},
	weight: 1,
  }

  // 表单验证规则
  const rules = {
    name: [{
      required: true,
      message: '请输入等级名称',
      trigger: ['blur', 'change']
    }],
    weight: [{
      required: true,
      message: '请输入等级名称',
      trigger: ['blur', 'change']
    }],
    expend_money: [{
      required: true,
      message: '请输入升级所需消费金额',
      trigger: ['blur', 'change']
    }],
  }

  export default {
    data() {
      return {
        form,
        rules,
        // 按钮禁用
        disabled: false
      }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
		this.form.name = ''
		this.form.weight = 1
	},

    // 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
    onReady() {
      this.$refs.uForm.setRules(this.rules)
    },

    methods: {

      // 表单提交
      handleSubmit() {
        const app = this
        if (app.disabled) {
          return false
        }
        app.$refs.uForm.validate(valid => {
          if (valid) {
            app.disabled = true
			app.form.weight = parseInt(app.form.weight)
            GradeApi.add(app.form)
              .then(result => {
                app.$toast(result.message)
                uni.navigateBack()
              })
              .finally(() => app.disabled = false)
          }
        })
      }

    }
  }
</script>

<style>
  page {
    background: #f7f8fa;
  }
</style>
<style lang="scss" scoped>
  .page-title {
    width: 94%;
    margin: 0 auto;
    padding-top: 40rpx;
    font-size: 28rpx;
    color: rgba(69, 90, 100, 0.6);
  }

  .form-wrapper {
    margin: 20rpx auto 20rpx auto;
    padding: 0 40rpx;
    width: 94%;
    box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
    border-radius: 16rpx;
    background: #fff;
  }

  /* 底部操作栏 */

  .footer {
    margin-top: 60rpx;

    .btn-wrapper {
      height: 100%;
      display: flex;
      align-items: center;
      padding: 0 20rpx;
    }

    .btn-item {
      flex: 1;
      font-size: 28rpx;
      height: 100rpx;
      line-height: 100rpx;
      text-align: center;
      color: #fff;
      border-radius: 50rpx;
    }

    .btn-item-main {
      background: linear-gradient(to right, #f9211c, #ff6335);

      // 禁用按钮
      &.disabled {
        background: #ff9779;
      }
    }

  }
</style>
